package com.yx.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.sys.entity.Edition;
import com.yx.sys.entity.Tags;
import com.yx.sys.entity.Wiki;
import com.yx.sys.entity.WikiPostVO;
import com.yx.sys.mapper.EditionMapper;
import com.yx.sys.mapper.TagsMapper;
import com.yx.sys.mapper.WikiMapper;
import com.yx.sys.service.IWikiService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 链接 服务实现类
 * </p>
 *
 * @author yx
 * @since 2023-02-14
 */
@Service
public class WikiServiceImpl extends ServiceImpl<WikiMapper, Wiki> implements IWikiService {

    @Autowired(required = false)
    private WikiMapper wikiMapper;

    @Autowired(required = false)
    private TagsMapper tagsMapper;

    @Autowired(required = false)
    private EditionMapper editionMapper;

    @Override
    public List<Wiki> getAllWiki(Integer startId,Integer pageSize) {
        List<Wiki> list = wikiMapper.selectAllWiki(startId,pageSize);
        return list;
    }

    @Override
    public List<Wiki> findPostWikiMes(Integer eId, Integer tId, Integer startId, Integer pageSize) {
//        Page<WikiPostVO> pageParam = new Page<>(startId,pageSize);
//        QueryWrapper<WikiPostVO> wrapper = new QueryWrapper<>();
////        wrapper.eq(ObjectUtil.isNotNull(eId),"eId",eId);
////        wrapper.eq("eId",eId).eq("tId",tId);
//        wrapper.eq(eId!=null,"w.eId",eId);
//        wrapper.eq(tId!=null,"w.tId",tId);
//        List<WikiPostVO> wikis = wikiMapper.selectPostWikiMes(pageParam, wrapper);
        List<Wiki> wikis = wikiMapper.selectPostWikiMes(eId, tId, startId, pageSize);
        return wikis;
    }

    @Override
    public Integer findWikiIsExist(String wName, String wLink, Integer tId, Integer eId) {
        Integer wId = wikiMapper.selectWikiIsExist(wName, wLink, tId, eId);
        return wId;
    }

    @Override
    public List<?> getWikiMes(Integer eId, Integer tId,Integer status) {
        List<?> list = wikiMapper.selectWikiMes(eId, tId,status);
        return list;
    }

    @Override
    public Map<String, Object> getWikiByWid(Integer wId) {
        QueryWrapper<Wiki> wrapper = new QueryWrapper<>();
        wrapper.eq("wId",wId);
        Wiki wiki = wikiMapper.selectOne(wrapper);
        Tags tags = tagsMapper.selectById(wiki.gettId());
        Edition edition = editionMapper.selectById(wiki.geteId());
        Map<String,Object> data = new HashMap<>();
        data.put("wId",wiki.getwId());
        data.put("wName",wiki.getwName());
        data.put("wLink",wiki.getwLink());
        data.put("wIcon",wiki.getwIcon());
        data.put("tName",tags.gettName());
        data.put("eName",edition.geteName());
        return data;
    }
}
